package com.sec.security;

import com.sec.dao.mysql.UserDao;
import com.sec.domain.mysql.User;
import com.sec.domain.mysql.UserAccessLog;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;

/**
 * 登录成功后跳转
 * Created by Administrator on 2017/10/31.
 */
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
    @Resource
    private UserDao userDao;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response, Authentication authentication) throws IOException {
        handle(request, response, authentication);
        clearAuthenticationAttributes(request);
    }

    protected void handle(HttpServletRequest request,
                          HttpServletResponse response, Authentication authentication) throws IOException {
        String targetUrl = determineTargetUrl(authentication);

        if (response.isCommitted()) {
            System.out.println("Response has already been committed. Unable to redirect to " + targetUrl);
            return;
        }

        redirectStrategy.sendRedirect(request, response, targetUrl);
    }


    protected String determineTargetUrl(Authentication authentication) {
        // 记录用户登录信息
        userAccessLog(authentication);
        return "/article/index.html";

    }

    /**
     * 记录用户登录和访问信息
     * */
    private void userAccessLog(Authentication authentication) {
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String username = userDetails.getUsername();

        // 记录登录信息
        com.sec.domain.mysql.User user = new com.sec.domain.mysql.User();
        user.setUsername(username);
        List<User> userList = userDao.queryByCon(user);
        User user1 = userList.get(0);
        if(user1.getFirstLoginTime()==null){ // 如果第一次登录，设置第一次登录时间
            user1.setFirstLoginTime(new Timestamp(System.currentTimeMillis()));
        }
        user1.setLastLoginTime(new Timestamp(System.currentTimeMillis())); // 设置最后登录时间
        userDao.update(user1);

    }

    protected void clearAuthenticationAttributes(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return;
        }
        session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
    }

    public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
        this.redirectStrategy = redirectStrategy;
    }
    protected RedirectStrategy getRedirectStrategy() {
        return redirectStrategy;
    }


}